package com.example.hjlquickstartdemo.pojo.vo;

import com.example.hjlquickstartdemo.utils.AppUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;


import javax.validation.constraints.NotNull;
import java.io.Serializable;


@Data
@AllArgsConstructor
@EqualsAndHashCode
@ApiModel(value = "ResultResponse",description = "公共应答体")
public class ResultResponse<T> implements Serializable {

    private static final long serialVersionUID = -6372561804247815227L;

    @ApiModel(value = "公共应答体头部包含的公共信息")
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static final class Head implements Serializable {

        private static final long serialVersionUID = 9068029931352525287L;
        /**
         *  应用程序版本，必填
         */
        @ApiModelProperty(value = "服务端应用版本号")
        @NotNull
        private String version;
        /**
         * 应答码，0 代表成功，失败则填写异常错误码
         */
        @ApiModelProperty(value = "0代表成功 -1代表没有影响记录，其他为业务异常码")
        private String code;
        /**
         * 消息的显示全部服务端定义
         */
        @ApiModelProperty(value = "成功或者错误消息")
        private String message;
        /**
         * 加密标志，1标记加密 0不加密
         */
        @ApiModelProperty(value = "加密标志暂时不启用")
         private Integer flag = 0;


    }

    /**
     * 应答报文头
     */
    @ApiModelProperty(value = "头部信息")
    private Head head;

    /***
     * 应答体可以为 基本字段类型也可以是对象或者分页列表
     */
    @ApiModelProperty(value = "报文体主要为业务数据")
    private T body;

    public ResultResponse() {
        this.head = new Head();
    }

    @Override
    public String toString() {
        return "ResultResponse{" +
                "head=" + head +
                ", body=" + body +
                '}';
    }

    public static <T> ResultResponseBuilder<T> builder() {
        return new ResultResponseBuilder<>();
    }

    public static final class ResultResponseBuilder<T> {

        private final Head head;

        private T body;

        private ResultResponseBuilder() {
            this.head = new Head();
        }

        public ResultResponseBuilder<T> withVersion(String version) {
            this.head.setCode(version);
            return this;
        }

        public ResultResponseBuilder<T> withCode(String code) {
            this.head.setCode(code);
            return this;
        }

        public ResultResponseBuilder<T> withMessage(String message) {
            this.head.setMessage(message);
            return this;
        }

        public ResultResponseBuilder<T> withFlag(int flag) {
            this.head.setFlag(flag);
            return this;
        }

        public ResultResponseBuilder<T> withBody(T body) {
            this.body = body;
            return this;
        }

        /**
         * Build result.
         *
         * @return result
         */
        public ResultResponse<T> build() {
            ResultResponse<T> ResultResponse = new ResultResponse();
            ResultResponse.setHead(head);
            if (StringUtils.isEmpty(ResultResponse.getHead().getVersion())) {
               AppUtils.setResponseExtendInfo(ResultResponse);
            }
            ResultResponse.setBody(body);
            return ResultResponse;
        }


    }
}
